package me.zhengjie.inter.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.AdlZhsq;
import me.zhengjie.business.domain.Whfa;
import me.zhengjie.business.domain.WhfaFfx;
import me.zhengjie.business.domain.Ymkdz;
import me.zhengjie.business.repository.WhfaFfxRepository;
import me.zhengjie.business.repository.WhfaRepository;
import me.zhengjie.business.repository.YmkdzRepository;
import me.zhengjie.business.service.dto.WhfaDto;
import me.zhengjie.business.service.dto.WhfaQueryCriteria;
import me.zhengjie.business.service.mapstruct.WhfaMapper;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.inter.service.MpWhfaService;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 服务接口
 * @author xiahongbo
 * @date 2022-07-18
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MpWhfaServiceImpl implements MpWhfaService {

    private final YmkdzRepository ymkdzRepository;
    private final WhfaFfxRepository whfaFfxRepository;
    private final WhfaRepository whfaRepository;
    private final WhfaMapper whfaMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WhfaDto create(Whfa resources) {

        List<WhfaFfx> whfaFfxList = resources.getWhfaFfxes();

        // 清空实体中服务项表信息
        resources.setWhfaFfxes(new ArrayList<>());
        if(StringUtils.isEmpty(resources.getBisWhfaId())){
            resources.setBisWhfaId(IdUtil.simpleUUID());
        }
        String fwlx = resources.getBisWhfaFwlx();

        // 01、94：生活照料；04、95：医疗护理
        fwlx = "01".equals(fwlx) ? "94" : "04".equals(fwlx) ? "95" : "";
        resources.setBisWhfaFwlx(fwlx);
        resources.setBisWhfaZt("0");
        Whfa newWhfa = whfaRepository.save(resources);
        // 保存之前先删除，按方案ID删除
        whfaFfxRepository.deleteByBisWhfaFaid(newWhfa.getBisWhfaId());
        for (WhfaFfx w : whfaFfxList) {

            w.setBisWhfaFfxSl("1");

            Ymkdz ymkdz = ymkdzRepository.findById(w.getBisWhfaDzid()).orElseGet(Ymkdz::new);
            w.setBisWhfaFfxId(IdUtil.simpleUUID());
            w.setWhfa(newWhfa);
            w.setBisWhfaFaid(newWhfa.getBisWhfaId());
            w.setBisWhfaZhid(newWhfa.getBisWhfaZhsqId());
            w.setBisWhfaFfxFwbm(ymkdz.getBisYmkdzYbbm());
            w.setBisWhfaFfxFwmc(ymkdz.getBisYmkdzMc());
            w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());
            w.setBisWhfaFfxZjm(ymkdz.getBisYmkdzZjm());
            w.setBisWhfaFfxDj(ymkdz.getBisYmkdzDj());
            w.setBisWhfaFfxFwlx(ymkdz.getBisYmkdzFwlx());
            w.setBisWhfaFfxJslx(ymkdz.getBisYmkdzJslx());

            w.setBisWhfaFfxYbmc(ymkdz.getBisYmkdzYbmc());
            w.setBisWhfaFfxYbdj(ymkdz.getBisYmkdzYbdj());
            w.setBisWhfaFfxYbzjm(ymkdz.getBisYmkdzYbzjm());
            w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());

            whfaFfxRepository.save(w);

        }

        return whfaMapper.toDto(newWhfa);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<WhfaDto> create(Whfa resources, String whrqs) {


        List<WhfaDto> list = new ArrayList<>();

        String month = "";
        String[] whrqArray = whrqs.split(",");
        for (int i = 0; i < whrqArray.length; i++) {

            month = whrqArray[i].substring(0, 7);
        }

        List<WhfaFfx> whfaFfxList = resources.getWhfaFfxes();
        // 清空实体中服务项表信息
        resources.setWhfaFfxes(new ArrayList<>());


        Long zhid = resources.getBisWhfaZhsqId();
        String fwlx = resources.getBisWhfaFwlx();

        // 01、94：生活照料；04、95：医疗护理
        fwlx = "01".equals(fwlx) ? "94" : "04".equals(fwlx) ? "95" : "";
        resources.setBisWhfaFwlx(fwlx);
        resources.setBisWhfaZt("0");

        int cnt = whfaRepository.checkFaCnt(zhid, month, fwlx);
        if(fwlx.equals("94")) {
            if(cnt > 8) {
                throw new BadRequestException("生活照料方案计划["+month+"]最多8条计划");
            }
        }
        if(fwlx.equals("95")) {
            if(cnt > 4) {
                throw new BadRequestException("医疗护理方案计划["+month+"]最多4条计划");
            }
        }

        for (int i = 0; i < whrqArray.length; i++) {

            String whrq = whrqArray[i];
            Whfa isExist = whfaRepository.queryByBisWhfaZhsqIdAndBisWhfaHwrq(zhid, whrq);
            if(isExist != null) {
                throw new BadRequestException("方案计划["+whrq+"]已经存在");
            }

            resources.setBisWhfaId(IdUtil.simpleUUID());
            resources.setBisWhfaHwrq(whrq);
            Whfa newWhfa = whfaRepository.save(resources);
            // 保存之前先删除，按方案ID删除
            // whfaFfxRepository.deleteByBisWhfaFaid(newWhfa.getBisWhfaId());
            for (WhfaFfx w : whfaFfxList) {

                w.setBisWhfaFfxSl("1");

                Ymkdz ymkdz = ymkdzRepository.findById(w.getBisWhfaDzid()).orElseGet(Ymkdz::new);
                w.setBisWhfaFfxId(IdUtil.simpleUUID());
                w.setWhfa(newWhfa);
                w.setBisWhfaFaid(newWhfa.getBisWhfaId());
                w.setBisWhfaZhid(newWhfa.getBisWhfaZhsqId());
                w.setBisWhfaFfxFwbm(ymkdz.getBisYmkdzYbbm());
                w.setBisWhfaFfxFwmc(ymkdz.getBisYmkdzMc());
                w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());
                w.setBisWhfaFfxZjm(ymkdz.getBisYmkdzZjm());
                w.setBisWhfaFfxDj(ymkdz.getBisYmkdzDj());
                w.setBisWhfaFfxFwlx(ymkdz.getBisYmkdzFwlx());
                w.setBisWhfaFfxJslx(ymkdz.getBisYmkdzJslx());

                w.setBisWhfaFfxYbmc(ymkdz.getBisYmkdzYbmc());
                w.setBisWhfaFfxYbdj(ymkdz.getBisYmkdzYbdj());
                w.setBisWhfaFfxYbzjm(ymkdz.getBisYmkdzYbzjm());
                w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());

                whfaFfxRepository.save(w);

            }
            WhfaDto whfaDto = whfaMapper.toDto(newWhfa);
            list.add(whfaDto);
        }

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WhfaDto update(Whfa resources) {
        List<WhfaFfx> whfaFfxList = resources.getWhfaFfxes();

        // 清空实体中服务项表信息
        resources.setWhfaFfxes(new ArrayList<>());

        Whfa whfa = whfaRepository.findById(resources.getBisWhfaId()).orElseGet(Whfa::new);
        whfa.copy(resources);
        whfaRepository.save(whfa);


        // 保存之前先删除，按方案ID删除
        whfaFfxRepository.deleteByBisWhfaFaid(whfa.getBisWhfaId());
        for (WhfaFfx w : whfaFfxList) {

            w.setBisWhfaFfxSl("1");

            Ymkdz ymkdz = ymkdzRepository.findById(w.getBisWhfaDzid()).orElseGet(Ymkdz::new);
            w.setBisWhfaFfxId(IdUtil.simpleUUID());
            w.setWhfa(whfa);
            w.setBisWhfaFaid(whfa.getBisWhfaId());
            w.setBisWhfaZhid(whfa.getBisWhfaZhsqId());
            w.setBisWhfaFfxFwbm(ymkdz.getBisYmkdzYbbm());
            w.setBisWhfaFfxFwmc(ymkdz.getBisYmkdzMc());
            w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());
            w.setBisWhfaFfxZjm(ymkdz.getBisYmkdzZjm());
            w.setBisWhfaFfxDj(ymkdz.getBisYmkdzDj());
            w.setBisWhfaFfxFwlx(ymkdz.getBisYmkdzFwlx());
            w.setBisWhfaFfxJslx(ymkdz.getBisYmkdzJslx());

            w.setBisWhfaFfxYbmc(ymkdz.getBisYmkdzYbmc());
            w.setBisWhfaFfxYbdj(ymkdz.getBisYmkdzYbdj());
            w.setBisWhfaFfxYbzjm(ymkdz.getBisYmkdzYbzjm());
            w.setBisWhfaFfxFwsc(ymkdz.getBisYmkdzFwsc());

            whfaFfxRepository.save(w);

        }
        return whfaMapper.toDto(whfa);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        whfaRepository.deleteById(id);
        whfaFfxRepository.deleteByBisWhfaFaid(id);
    }
}
